<?php
/**
 * Created by JetBrains PhpStorm.
 * User: huan
 * Date: 14-3-11
 * Time: 上午10:37
 * To change this template use File | Settings | File Templates.
 */

class Sms {

    //id
    private $userid     = 7817;
    private $account    = 'koubei';
    private $password   = 'kb159357';

    //接口地址
    private $api_url = 'http://www.duanxin10086.com/sms.aspx?';

    function __construct(){
        import("@.ORG.Snoopy");
    }
    function send($mobile,$msg,$type='消息'){

        $send_result = $this->api_send($mobile,$msg);

        return $send_result;
    }


    /**
     * 发送短信
     * @param $mobile
     * @param $msg
     */
    function api_send($mobile,$msg){

        $params = array(
            'action'        => 'send',
            'userid'        => $this->userid,
            'account'       => $this->account,
            'password'      => $this->password,
            'mobile'        => $mobile,
            'content'       => $msg,
            'sendTime'      =>'',                   //定时发送时间	为空表示立即发送，定时发送格式2010-10-24 09:08:10
            'taskName'      =>'',                   //任务名称	本次任务 描述100个字以内  可以为空
            'checkcontent'  => 1,                   //是否检查内容包含非法关键字	当设置为1时表示需要检查，默认0为不检查
            'mobilenumber'  => 1,                   //手机号码数量	本次提交的手机号码数量
            'countnumber'   => 1,                   //号码总数量	本次提交的号码总数量
            'telephonenumber'=> 0                   //小灵通或座机号码数	本次提交的小灵通或座机号码数数量
        );

        $xml = $this->api_get($params);

        return $this->api_callback($params['action'],$xml);
    }


    /**
     * 余额
     */
    function api_overage(){

        /**
         * userid	企业id	企业ID
        account	用户帐号	用户帐号，由系统管理员
        password	帐号密码	用户账号对应的密码
        action	任务名称	设置为固定的：overage
         */


        $params = array(
            'action'        => 'overage',
            'userid'        => $this->userid,
            'account'       => $this->account,
            'password'      => $this->password
        );

        $xml = $this->api_get($params);

        return $this->api_callback($params['action'],$xml);
    }

    /**
     * 检查关键字
     */
    function api_checkkeyword(){

    }

    function api_query(){

    }

    /**
     * 调用接口
     * @param $params
     * @return SimpleXMLElement
     */
    function api_get($params){

        $url = $this->api_url.http_build_query($params);
        $cSnoopy = new Snoopy();
        $cSnoopy->fetch($url);

        $result = $cSnoopy->results;

        $xml = simplexml_load_string($result);

        return json_decode(json_encode($xml),TRUE);
    }

    /**
     * 回调函数
     * @param $act
     * @param $oXml
     */
    function api_callback($act,$return){

        if(!isset($return['returnstatus'])){
            return 0;
        }

        if($act == 'send'){
            //发送
            return array(
                'message'           => $return['message'],
                'remainpoint'       => $return['remainpoint'],
                'taskID'            => $return['taskID'],
                'successCounts'     => $return['successCounts']
            );
        }else if($act == 'overage'){
            //余额
            return array(
                'payinfo'   => $return['payinfo'],
                'overage'   => $return['overage'],
                'sendTotal' => $return['sendTotal']
            );
        }
    }
}
?>